package com.xpgk.xpsoft.task;

import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.jfinal.kit.PathKit;
import com.jfinal.kit.PropKit;
import com.jfinal.log.Log;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.DbKit;
import com.jfinal.plugin.activerecord.Record;
import com.xpgk.common.kit.CacheKitEx;
import com.xpgk.common.kit.MailKit;
import com.xpgk.common.kit.PasswordKit;
import com.xpgk.common.kit.PubKit;
import com.xpgk.xpsoft.common.XPSoftLog;
import com.xpgk.xpsoft.constants.XPSoftConstant;
import com.xpgk.xpsoft.system.SystemService;

public class TaskSecurity implements Runnable {

	private Log log = Log.getLog(getClass());

	// 定时检测系统安全问题，及磁盘使用情况，及数据库使用情况
	@Override
	public void run() {
		// TODO Auto-generated method stub
		log.info("start run security task.");

		// 磁盘使用情况
		File file = new File(PathKit.getWebRootPath());
		if (file.exists()) {
			long freeSpace = file.getFreeSpace() / (1024 * 1024);
			if (freeSpace < 1024 * 2) // 小于1G
			{
				log.warn("硬盘空间已经小于2G.");

				String host = PropKit.get("email_host"); // 发送邮件的服务器的IP
				String port = PropKit.get("email_port"); // 发送邮件的服务器的端口

				String from = PropKit.get("email_from"); // 邮件发送者的地址
				String emailuserName = PropKit.get("email_userName"); // 登陆邮件发送服务器的用户名
				String emailpassword = PasswordKit.getPassword("email_password"); // 登陆邮件发送服务器的密码

				List<String> to = new ArrayList<String>(); // 邮件接收者的地址
				to.add(from);

				boolean validate = true; // 是否需要身份验证

				Record record = SystemService.GetSystem();
				String subject = "";
				if (record != null) {
					subject = record.getStr("name");
				}
				subject = subject + ",系统报警通知"; // 邮件标题
				String content = "尊敬的管理员：硬盘空间已经小于2G,请及时处理"; // 邮件的文本内容
				String[] attachFileNames = null;// new String[]{"D:/code.jpg"};
												// // 邮件附件的文件名
				XPSoftLog.write(1, "系统报警通知", content, XPSoftConstant.log_system_alter, 1, "系统报警通知");
				MailKit.sendTextMail(host, port, validate, emailuserName, emailpassword, from, to, subject, content,
						attachFileNames);
			}
		}

		// 检查数据库中是否有非法数据

		// 检查数据库表空间是否满
		if (DbKit.getConfig().getDialect().isOracle()) {
			String sql = "select tablespace_name, sum(bytes)/1024/1024 AS freeSpace from dba_free_space group BY tablespace_name Having tablespace_name='SYSTEM'";
			String tableSpaceName = PropKit.get("user_tableSpaceName");
			List<Record> list = null;
			if (!PubKit.isBlank(tableSpaceName)) {
				sql += " or tablespace_name=?";
				list = Db.find(sql, tableSpaceName);
			} else
				list = Db.find(sql);
			boolean bSpaceNotEnough = false;
			for (Iterator<Record> iterator = list.iterator(); iterator.hasNext();) {
				Record record = (Record) iterator.next();
				if (record.getInt("freeSpace") < 100) {
					bSpaceNotEnough = true;
				}
			}
			if (bSpaceNotEnough) {
				String host = PropKit.get("email_host"); // 发送邮件的服务器的IP
				String port = PropKit.get("email_port"); // 发送邮件的服务器的端口

				String from = PropKit.get("email_from"); // 邮件发送者的地址
				String emailuserName = PropKit.get("email_userName"); // 登陆邮件发送服务器的用户名
				String emailpassword = PasswordKit.getPassword("email_password"); // 登陆邮件发送服务器的密码

				List<String> to = new ArrayList<String>(); // 邮件接收者的地址
				to.add(from);

				boolean validate = true; // 是否需要身份验证

				Record record = SystemService.GetSystem();
				String subject = "";
				if (record != null) {
					subject = record.getStr("name");
				}
				subject = subject + ",系统报警通知"; // 邮件标题
				String[] attachFileNames = null;
				String content = "尊敬的管理员：表空间小于100M.,请及时处理";

				XPSoftLog.write(1, "系统报警通知", content, XPSoftConstant.log_system_alter, 1, "系统报警通知");
				log.warn("表空间小于100M.");
				MailKit.sendTextMail(host, port, validate, emailuserName, emailpassword, from, to, subject, content,
						attachFileNames);
			}
		}

		// 清理任务记录
		CacheKitEx.put(XPSoftConstant.cacheKey_System, XPSoftConstant.cacheKey_Handle_RunState_Enable, 0);
		log.info("stop run security task.");
	}

}
